package mesweb.mesweb.controller.sale;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import com.power.common.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.sale.SaleOrderProductNoDtl;
import mesweb.mesweb.service.sale.ISaleOrderProductNoDtlService;
import mesweb.mesweb.vo.sale.OrderProudctDtlVo;
import mesweb.mesweb.vo.sale.ProcedureProductQty;
import mesweb.mesweb.vo.sale.SaleOrderProductNoDtlVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 订单产品关系
 * @Author: jeecg-boot
 * @Date: 2021-07-02
 * @Version: V1.0
 */

@Api(tags = "订单产品关系")
@RestController
@RequestMapping("/mes/saleOrderProductNoDtl")
public class SaleOrderProductNoDtlController {
    @Autowired
    private ISaleOrderProductNoDtlService saleOrderProductNoDtlService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam(value = "name", required = false) String name) {
        List<SaleOrderProductNoDtlVo> data = new ArrayList<>();
        QueryWrapper<SaleOrderProductNoDtlVo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
        }
        data = saleOrderProductNoDtlService.getsaleOrderProductNoDtl(queryWrapper);
        return CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param saleOrderProductNoDtl
     * @param page
     * @param size
     * @param req
     * @return
     */
    @ApiOperation(value = "订单产品关系-分页列表查询", notes = "订单产品关系-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(SaleOrderProductNoDtlVo saleOrderProductNoDtl,
                                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                                         @RequestParam(name = "size", defaultValue = "10") Integer size,
                                         HttpServletRequest req) {
        QueryWrapper<SaleOrderProductNoDtlVo> queryWrapper = new QueryWrapper<>();
        Page<SaleOrderProductNoDtlVo> pageSaleOrderProductNoDtl = new Page<>(page, size);
        IPage<SaleOrderProductNoDtlVo> pageList = saleOrderProductNoDtlService.getsaleOrderProductNoDtl(pageSaleOrderProductNoDtl, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑
     *
     * @param saleOrderProductNoDtl
     * @return
     */
    @ApiOperation(value = "订单产品关系-编辑", notes = "订单产品关系-编辑")
    @PostMapping(value = "/save")
    public CommonResult<?> edit(@RequestBody SaleOrderProductNoDtl saleOrderProductNoDtl) {
        boolean result = false;
        if (saleOrderProductNoDtl.getId() == 0L) {
            QueryWrapper<SaleOrderProductNoDtl> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sdorderId", saleOrderProductNoDtl.getSdorderId());
            queryWrapper.or().eq("productId", saleOrderProductNoDtl.getProductId());
            SaleOrderProductNoDtl mmMaterialdtl1 = saleOrderProductNoDtlService.getOne(queryWrapper);
            if (mmMaterialdtl1 != null) {
                String msg = StrUtil.format("编号为{}或者名稱为{}的记录已经存在！", mmMaterialdtl1.getSdorderId(),mmMaterialdtl1.getProductId());
                throw new ApiException(msg);
            }

            result = saleOrderProductNoDtlService.save(saleOrderProductNoDtl);
        } else {
            result = saleOrderProductNoDtlService.updateById(saleOrderProductNoDtl);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "订单产品关系-通过id删除", notes = "订单产品关系-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name = "id", required = true) String id) {
        saleOrderProductNoDtlService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "订单产品关系-批量删除", notes = "订单产品关系-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("SaleOrderProductNoDtlIDs") @RequestBody List<Long> ids) {
        this.saleOrderProductNoDtlService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "订单产品关系-通过id查询", notes = "订单产品关系-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SaleOrderProductNoDtl saleOrderProductNoDtl = saleOrderProductNoDtlService.getById(id);
        return CommonResult.success(saleOrderProductNoDtl);
    }


    /**
     * 分页列表查询
     *
     * @param productProcedureNo
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "订单产品关系-分页列表查询", notes = "订单产品关系-分页列表查询")
    @GetMapping(value = "/getProcedureProductQty")
    public CommonResult<?> getProcedureProductQty(
                                                  @RequestParam(name = "productProcedureNo", required = false) String productProcedureNo,
                                                  @RequestParam(name = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(name = "size", defaultValue = "10") Integer size
                                                  ) {
        QueryWrapper<ProcedureProductQty> queryWrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(productProcedureNo))
        {
            queryWrapper.eq("c.Product_Procedure_No",productProcedureNo);
        }
        Page<ProcedureProductQty> procedureProductQtyPage = new Page<>(page, size);
        IPage<ProcedureProductQty> pageList = saleOrderProductNoDtlService.getProcedureProductQty(procedureProductQtyPage, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 分页查询订单产品产线列表
     * */
    @ApiOperation(value = "订单产品关系-分页列表查询", notes = "订单产品关系-分页列表查询")
    @GetMapping(value = "/getOrderProductDtlVo")
    public CommonResult<?> getOrderProductDtlVo(
            @RequestParam(name = "orderNo", required = false) String orderNo,
            @RequestParam(name = "productNo", required = false) String productNo,
            @RequestParam(name = "deliveryStartDate", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date deliveryStartDate,
            @RequestParam(name = "deliveryEndDate", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date deliveryEndDate,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        QueryWrapper<OrderProudctDtlVo> queryWrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(orderNo))
        {
            queryWrapper.eq("so.OrderNo",orderNo);
        }
        if(StringUtil.isNotEmpty(productNo))
        {
            queryWrapper.eq("son.Product_No",productNo);
        }
        if(StringUtil.isNotEmpty(DateUtil.format(deliveryStartDate,"yyyy-MM-dd HH:mm:ss")))
        {
            queryWrapper.gt("so.dRefDeliveryDate", new Timestamp(deliveryStartDate.getTime()));
        }
        if(StringUtil.isNotEmpty(DateUtil.format(deliveryEndDate,"yyyy-MM-dd HH:mm:ss")))
        {
            queryWrapper.le("so.dRefDeliveryDate",new Timestamp(deliveryEndDate.getTime()));
        }
        Page<OrderProudctDtlVo> orderProudctDtlVoPage = new Page<>(page, size);
        IPage<OrderProudctDtlVo> pageList = saleOrderProductNoDtlService.getOrderProductDtlVo(orderProudctDtlVoPage, queryWrapper);
        return CommonResult.success(pageList);
    }
}
